#include "LoopThread.hpp"

class LoopThreadPool
{
private:
    int _thread_count;    //从属线程的数量 
    int _next_idx;   // 索引
    EventLoop *_baseLoop;  //主Reactor线程, 如果从属线程线程为0， 则所有操作都在baseloop
    std::vector<LoopThread*> _threads; // 保存所有的LoopThread对象的
    std::vector<EventLoop*> _loops;

public:
    LoopThreadPool(EventLoop *baseloop) :_thread_count(0), _next_idx(0), _baseLoop(baseloop){}
    //设置线程数量
    void SetThreadCount(int count) { _thread_count = count; }
    //创建_thread_count个从属Reactor
    void CreateCountReactor()
    {
        if(_thread_count > 0)
        {
            _threads.resize(_thread_count);
            _loops.resize(_thread_count);
            for(int i = 0; i < _thread_count; i++)
            {
                _threads[i] = new LoopThread();
                _loops[i] = _threads[i]->GetLoop();
            }
        }
    }
    EventLoop* NextLoop()
    {
        if(_thread_count == 0)
            return _baseLoop;
            
        _next_idx = (_next_idx + 1) % _thread_count;
        return _loops[_next_idx];
    }
};